home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / 3dkit1.zip / 2DTOHPGL.C < prev    next >
Text File  |  1992-06-28  |  4KB  |  133 lines

  1. /* 2DtoHPGL.c  -  convert 2-D list to HPGL plotter commands */
  2.  
  3. /* Oscar Garcia <garciao@mof.govt.nz>, May 1992 */
  4.  
  5. /* uses getopt */
  6. int getopt(int argc, char *argv[], char *options);
  7. extern  int optind;
  8. extern char *optarg;
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15.  
  16. #define USAGE "usage: 2DtoHPGL [/s] [/xn] [/yn] [/wn] [/hn] [/pn]\
  17.  [input_file [output_file]]\n\
  18. \tIf file names are omitted, the standard i/o streams are used.\n\
  19. \tOptions (with defaults, numbers in plotter units):\n\
  20. \t\t/s       -  Fit full (-1,-1) to (1,1) square\n\
  21. \t\t            (default is maximum magnification)\n\
  22. \t\t/x5450   -  Center x coordinate\n\
  23. \t\t/y3825   -  Center y coordinate\n\
  24. \t\t/w10000  -  Maximum width\n\
  25. \t\t/h7200   -  Maximum height\n\
  26. \t\t/p123456781234567  -  Mapping colours to pens\n"
  27.  
  28. #define ERROR(msg) {fputs(msg,stderr),exit(1);}
  29. #define PERROR(msg) {perror(msg),exit(1);}
  30.  
  31. #define RECLEN 81
  32.  
  33. void main(int argc, char* argv[])
  34. {
  35.     int i, c, n, currpen = 0, pen[] = {0,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7};
  36.     int cenx=5450, ceny=3825, width=5000, height=3600, square=0;
  37.                 /* HALF of width and height, actually */
  38.     double x, y, xmin=-1, xmax=1, ymin=-1, ymax=1;    /* if square */
  39.     char comment[RECLEN + 1], *s;
  40.     char options[] = "x:y:w:h:sp:";
  41.     FILE *infile = stdin, *outfile = stdout;
  42.  
  43.  
  44.     /* get options */
  45.     while ((c = getopt(argc, argv, options)) != EOF)
  46.         if (c == '?')
  47.             ERROR(USAGE)
  48.         else
  49.             switch (c)
  50.             {    case 'x':    cenx = atoi(optarg); break;
  51.                 case 'y':    ceny = atoi(optarg); break;
  52.                 case 'w':    width = atoi(optarg) / 2; break;
  53.                 case 'h':    height = atoi(optarg) / 2; break;
  54.                 case 's':    square = 1; break;
  55.                 case 'p':
  56.                     i = 0;
  57.                     while (isdigit(n = *(optarg + i)))
  58.                         pen[++i] = n;
  59.                     break;
  60.             }
  61.  
  62.     /* open files */
  63.     if (optind < argc - 2)
  64.         ERROR(USAGE);    /* too many */
  65.     if (optind < argc)
  66.     {    infile = fopen(argv[optind], "rt");
  67.         if (infile == NULL)
  68.             ERROR("Can't open input file");
  69.         if (++optind < argc)
  70.         {    outfile = fopen(argv[optind], "wt");
  71.             if (outfile == NULL)
  72.                 ERROR("Can't open output file");
  73.         }
  74.     }
  75.  
  76.     /* get bounds, if necessary */
  77.     if (!square)
  78.     {    fgets(comment, RECLEN, infile);
  79.         s = strstr(comment, "x_range");
  80.         if (s == NULL || sscanf(s, "x_range=[%lf:%lf]", &xmin, &xmax) != 2)
  81.             ERROR("Bad or invalid x_range");
  82.         s = strstr(comment, "y_range");
  83.         if (s == NULL || sscanf(s, "y_range=[%lf:%lf]", &ymin, &ymax) != 2)
  84.             ERROR("Bad or invalid y_range");
  85.     }
  86.  
  87.     /* adjust aspect-ratio */
  88.     if (height * (xmax - xmin) > width * (ymax - ymin))
  89.         height = width * (ymax - ymin) / (xmax - xmin);
  90.     else
  91.         width = height * (xmax - xmin) / (ymax - ymin);
  92.  
  93.     /* set defaults, and plotter P1 and P2 */
  94.     fprintf(outfile, "DF;\nIP %d,%d,%d,%d;\n",
  95.         cenx - width, ceny - height, cenx + width, ceny + height);
  96.  
  97.     /* set scale */
  98.     fprintf(outfile, "SC %.4f,%.4f,%.4f,%.4f;\n",
  99.             100 * xmin, 100 * xmax, 100 * ymin, 100 * ymax);
  100.                     /* (scale by 100 for better precision) */
  101.  
  102.     /* skip comments */
  103.     while ((c = getc(infile)) == '#')
  104.         fgets(comment, RECLEN, infile);
  105.     if (ferror(infile))
  106.         PERROR("Bad input");
  107.     ungetc(c, outfile);
  108.  
  109.     /* do it */
  110.     while (3 == fscanf(infile, "%lf %lf %d", &x, &y, &c))
  111.         if (c == 0)
  112.             /* move */
  113.             fprintf(outfile, "PU %.4f,%.4f;\n", 100 * x, 100 * y);
  114.         else
  115.         {    /* draw */
  116.             if (currpen != pen[c])
  117.             {    /* new pen */
  118.                 currpen = pen[c];
  119.                 fprintf(outfile, "SP %d;\n", currpen);
  120.             }
  121.             fprintf(outfile, "PD %.4f,%.4f;\n", 100 * x, 100 * y);
  122.         }
  123.     fputs("PU;SP;", outfile);    /* put pen away when finished */
  124.  
  125.     if (ferror(infile))
  126.         PERROR("Error on input");
  127.     fclose(infile);
  128.  
  129.     if (ferror(outfile))
  130.         PERROR("Error on output");
  131.     fclose(outfile);
  132. }
  133.